package client;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;

import client.functionalities.ConfigurationFunctionality;
import client.functionalities.GetFriendsFunctionality;
import client.functionalities.SendScrapFunctionality;
import client.screen.AlertScreen;
import client.screen.ConfigurationScreen;
import client.screen.FriendsScreen;
import client.screen.MainScreen;
import client.screen.ScrapScreen;
import client.screen.ScraptextScreen;

final public class MorcMIDlet extends MIDlet {

	private Display display;
	private MainScreen mainScreen;
	private ScrapScreen scrapScreen;
	private ScraptextScreen scraptextScreen;
	private AlertScreen alertScreen;
	private FriendsScreen friendsScreen;
	private ConfigurationScreen confScreen;
	private String username;
	private String password;
	
	public static final int SCREEN_MAIN = 0;
	public static final int SCREEN_SCRAP = 1;
	public static final int SCREEN_SCRAPTEXT = 2;
	public static final int SCREEN_FRIENDS = 3;
	public static final int SCREEN_CONFIGURATION = 4;
	
	public MorcMIDlet() {
		display = Display.getDisplay(this);
		mainScreen = new MainScreen(this, "MobileOrkut");
		alertScreen = new AlertScreen("Aguarde...");
		friendsScreen = new FriendsScreen(this, "Amigos", List.EXCLUSIVE);
		confScreen = new ConfigurationScreen(this, "Configuracao");
		scraptextScreen = new ScraptextScreen(this, "Enviar recado");

		new ConfigurationFunctionality(confScreen).setFields();
		scrapScreen = new ScrapScreen(this, "Enviar recado");
	}
	
	protected void startApp() throws MIDletStateChangeException { 
		this.display.setCurrent(mainScreen);
	}
	
	public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
		this.notifyDestroyed();
	}

	protected void pauseApp() { }

	public void setScreen(int screenConstant) {
		if (screenConstant == MorcMIDlet.SCREEN_SCRAP) {
			this.display.setCurrent(this.scrapScreen);
		}
		else if (screenConstant == MorcMIDlet.SCREEN_MAIN) {
			this.display.setCurrent(this.mainScreen);
		}
		else if (screenConstant == MorcMIDlet.SCREEN_SCRAPTEXT) {
			this.display.setCurrent(this.scraptextScreen);
		}
		else if (screenConstant == MorcMIDlet.SCREEN_FRIENDS) {
			this.display.setCurrent(this.friendsScreen);
		}
		else if (screenConstant == MorcMIDlet.SCREEN_CONFIGURATION) {
			this.display.setCurrent(this.confScreen);
		}
	}
	
	public String getUser() { return this.username; }
	
	public String getPassword() { return this.password; }
	
	public String getRecipient() { return this.scrapScreen.getRecipient(); }
	
	public void getFriends() {
		GetFriendsFunctionality g = new GetFriendsFunctionality(this);
		this.alertScreen.newAlertThread(g, "Recebendo lista de amigos, aguarde...");
		this.alertScreen.startThread();
		
		// TODO revisar isso
		String[] s = (String[]) g.data;
		
		for (int i = 0; i < s.length; i++) {
			this.friendsScreen.deleteAll();
			this.friendsScreen.append(s[i], null);
		}
	}
	
	public void sendScrap(String content) { 
		SendScrapFunctionality s = new SendScrapFunctionality(this, content);
		this.alertScreen.newAlertThread(s, "Enviando scrap, aguarde...");
		this.alertScreen.startThread();
		
		Boolean b = (Boolean) s.data;
	}
	
	public void configure(String user, String pass) {
		ConfigurationFunctionality c = new ConfigurationFunctionality(this.confScreen);
		this.alertScreen.newAlertThread(c, "Salvando configuracao... aguarde");
		this.alertScreen.startThread();
	}

	public void setRecipient(String selectedFriend) { this.scrapScreen.setRecipient(selectedFriend); }

	public void setUser(String username) { this.username = username; }

	public void setPassword(String password) { this.password = password; }

}
